package com.egdigital.allzic.utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.egdigital.allzic.R;

public class AppRater {

	private final static String	APP_TITLE				= " Allzic Radio";
	private final static String	APP_PNAME				= "com.egdigital.allzic";
	private final static int	DAYS_UNTIL_PROMPT		= 0;
	private final static int	LAUNCHES_UNTIL_PROMPT	= 1;

	public static void app_launched(Context context) {
		SharedPreferences prefs = context.getSharedPreferences("apprater", 0);
		if (prefs.getBoolean("dontshowagain", false)) {
			return;
		}
		SharedPreferences.Editor editor = prefs.edit();
		// Increment launch counter
		long launch_count = prefs.getLong("launch_count", 0) + 1;
		editor.putLong("launch_count", launch_count);
		// Get date of first launch
		Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
		if (date_firstLaunch == 0) {
			date_firstLaunch = System.currentTimeMillis();
			editor.putLong("date_firstlaunch", date_firstLaunch);
		}
		// Wait at least n days before opening
		if (launch_count >= LAUNCHES_UNTIL_PROMPT) {
			if (System.currentTimeMillis() >= date_firstLaunch + (DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000)) {
				showRateDialog(context, editor);
			}
		}
		editor.commit();
	}

	private static void showRateDialog(final Context context, final SharedPreferences.Editor editor) {
		// with dialog
		//
		// final Dialog dialog = new Dialog(context);
		// dialog.setTitle(context.getString(R.string.rater_rate) + APP_TITLE);
		// dialog.setCancelable(false);
		// LinearLayout dialogLayout = new LinearLayout(context);
		// dialogLayout.setOrientation(LinearLayout.VERTICAL);
		// TextView dialogTitle = new TextView(context);
		// dialogTitle.setText(String.format(context.getString(R.string.rater_sent), APP_TITLE));
		// dialogTitle.setPadding(8, 10, 8, 10);
		// dialogLayout.addView(dialogTitle);
		// Button b1 = new Button(context);
		// b1.setText(context.getString(R.string.rater_rate));
		// b1.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
		// dialog.dismiss();
		// }
		// });
		// dialogLayout.addView(b1);
		// Button b2 = new Button(context);
		// b2.setText(context.getString(R.string.rater_later));
		// b2.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// dialog.dismiss();
		// }
		// });
		// dialogLayout.addView(b2);
		// Button b3 = new Button(context);
		// b3.setText(context.getString(R.string.rater_no));
		// b3.setOnClickListener(new View.OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// if (editor != null) {
		// editor.putBoolean("dontshowagain", true);
		// editor.commit();
		// }
		// dialog.dismiss();
		// }
		// });
		// dialogLayout.addView(b3);
		// dialog.setContentView(dialogLayout);
		// dialog.show();
		//
		// with alertdialog
		//
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(context.getString(R.string.rater_rate) + APP_TITLE);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setMessage(String.format(context.getString(R.string.rater_sent), APP_TITLE));
		builder.setPositiveButton(context.getString(R.string.rater_rate), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int id) {
				context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
				dialog.dismiss();
			}
		});
		builder.setNeutralButton(context.getString(R.string.rater_later), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int id) {
				dialog.dismiss();
			}
		});
		builder.setNegativeButton(context.getString(R.string.rater_no), new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int id) {
				if (editor != null) {
					editor.putBoolean("dontshowagain", true);
					editor.commit();
				}
				dialog.dismiss();
			}
		});
		builder.show();
	}
}
